//
//  TIMChatListCell.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/28.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMChatListCell.h"
#import "TIMMessageCountLabel.h"
#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMDemoMacro.h"
#import "TIMCommon.h"

#import "NSDate+TPSChat.h"

@interface TIMChatListCell ()

@property (nonatomic, strong) TIMMessageCountLabel *messageCountLabel;
@property (nonatomic,strong) UIImageView *icon;
@property (nonatomic,strong) UILabel *nameLab;
@property (nonatomic,strong) UILabel *contentLab;
@property (nonatomic,strong) UILabel *timeLab;
@property (nonatomic,strong) UIImageView *disturbImg;

@end

@implementation TIMChatListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configUI];
    }
    return self;
}

- (void)configUI{
    self.selectedBackgroundView = [[UIView alloc] init];
    self.selectedBackgroundView.backgroundColor = RGBAColor(238, 238, 238, 1);
    [self.contentView addSubview:self.icon];
    [self.contentView addSubview:self.messageCountLabel];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.contentLab];
    [self.contentView addSubview:self.timeLab];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.disturbImg];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    // 头像
    self.icon.frame = Frame(15, 10, 45, 45);
    self.icon.layer.cornerRadius = self.icon.frame.size.height/2;
    // 消息数量
    self.messageCountLabel.frame = Frame(CGRectGetMaxX(self.icon.frame) - 17/2 + 7, self.icon.frame.origin.y - 17/2 + 10, 17, 17);
    self.messageCountLabel.center = CGPointMake(CGRectGetMaxX(self.icon.frame), CGRectGetMinY(self.icon.frame));
    if (self.messageCountLabel.messageCount > 9 && self.messageCountLabel.messageCount < 100) {
        self.messageCountLabel.frame = Frame(CGRectGetMaxX(self.icon.frame) - 17/2, self.icon.frame.origin.y - 17/2, 22, 17);
    }else if(self.messageCountLabel.messageCount > 100){
        self.messageCountLabel.frame = Frame(CGRectGetMaxX(self.icon.frame) - 17/2, self.icon.frame.origin.y - 17/2, 26, 17);
        self.messageCountLabel.text = @"99+";
    } else if (self.messageCountLabel.messageCount < 1) {
        self.messageCountLabel.frame = Frame(CGRectGetMaxX(self.icon.frame) - 17/2, self.icon.frame.origin.y - 17/2, 0.01, 17);
    }
    // 名称
    self.nameLab.frame = Frame(CGRectGetMaxX(self.icon.frame) + 15, 14, 160, 17);
    // 时间戳
    self.timeLab.frame = Frame(CGRectGetMaxX(self.nameLab.frame), self.nameLab.frame.origin.y + 4, self.frame.size.width - 15 - CGRectGetMaxX(self.nameLab.frame), 14);
    self.lineView.frame = Frame(self.contentLab.frame.origin.x, self.contentView.frame.size.height - 1, self.frame.size.width - self.contentLab.frame.origin.x, 1);
    //
    self.disturbImg.frame = Frame(self.frame.size.width - 15 - self.disturbImg.image.size.width, CGRectGetMaxY(self.nameLab.frame) + 8, self.disturbImg.image.size.width, self.disturbImg.image.size.height);
    // 消息内容
    self.contentLab.frame = Frame(self.nameLab.frame.origin.x, CGRectGetMaxY(self.nameLab.frame) + 6, self.frame.size.width - self.nameLab.frame.origin.x - 15 - 15 - self.disturbImg.frame.size.width, 14);
    
}

- (void)setConversation:(TIMConversationInfo *)conversation {
    // 消息体
    _conversation = conversation;
    // 消息体类型
    if (conversation.convType == TIMConversationTypeGroup) {
        self.icon.layer.masksToBounds = NO;
        if (conversation.avatarPath.length > 0) {
            // 有上传头像
            [self.icon sd_setImageWithURL:[NSURL URLWithString:conversation.avatarPath] placeholderImage:[UIImage imageNamed:@"Group_infoImg"]];
        } else {
            // 没有上传头像
            self.icon.image = [UIImage imageNamed:@"Group_infoImg"];
        }
        
    } else {
        self.icon.layer.masksToBounds = YES;
        if (conversation.avatarPath.length > 0) {
            // 有上传头像
            [self.icon sd_setImageWithURL:[NSURL URLWithString:conversation.avatarPath] placeholderImage:[UIImage imageNamed:@"IM_User_infoImg"]];
        } else {
            // 没有上传头像
            self.icon.image = [UIImage imageNamed:@"IM_User_infoImg"];
        }
    }
    
    //
    if (conversation.partnerName.length > 0) {
        if (conversation.convType == TIMConversationTypePersonal) {
            [self.nameLab setText:conversation.partnerName];
        } else {
            [self.nameLab setText:conversation.partnerName];
        }
    } else {
        if (conversation.convType == TIMConversationTypeGroup) {
            [self.nameLab setText:@"群聊"];
        } else {
            TIMUserInfo *userFriend = [[TIMStorageManager sharedStorageManager] getFriendByFriendID:conversation.partnerID];
            [self.nameLab setText:userFriend.showName.length > 0 ? userFriend.showName : conversation.partnerID];
        }
    }
    
    [self.contentLab setText:conversation.content];
    
    [self.timeLab setText:conversation.date.conversaionTimeInfo];
    self.messageCountLabel.messageCount = _conversation.unreadCount;
    if ([conversation.stickState isEqualToString:@"YES"]) {
        self.contentView.backgroundColor = RGBAColor(248, 248, 248, 1);;
    } else {
        self.contentView.backgroundColor = UIColor.whiteColor;
    }
    
    if ([conversation.disturb isEqualToString:@"NO"]) {
        self.disturbImg.hidden = NO;
    } else {
        self.disturbImg.hidden = YES;
    }
}

#pragma mark - 懒加载

- (UIImageView *)icon
{
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
        _icon.image = [UIImage imageNamed:@"chatDisturb_image"];
    }
    return _icon;
}

- (TIMMessageCountLabel *)messageCountLabel {
    if (!_messageCountLabel) {
        _messageCountLabel = [[TIMMessageCountLabel alloc] init];
        _messageCountLabel.layer.cornerRadius = 17/2.0;
        _messageCountLabel.layer.masksToBounds = YES;
    }
    return _messageCountLabel;
}

- (UILabel *)nameLab{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.font = [UIFont boldSystemFontOfSize:16];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.textColor = UIColor.blackColor;
        //        _nameLab.text = @"Aron";
    }
    return _nameLab;
}

- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] init];
        _contentLab.font = TPSFontSize(12);
        _contentLab.textAlignment = NSTextAlignmentLeft;
        _contentLab.textColor = kSecondaryFontColor;
        //        _contentLab.text = @"我们三在床上躺一上午了都，起来啦！";
    }
    return _contentLab;
}

- (UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [[UILabel alloc] init];
        _timeLab.font = TPSFontSize(10);
        _timeLab.textAlignment = NSTextAlignmentRight;
        _timeLab.textColor = kAccupyFontColor;
        //        _timeLab.text = @"2017-12-12 00:00:00";
    }
    return _timeLab;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = RGBAColor(243, 243, 243, 1);
    }
    return _lineView;
}

- (UIImageView *)disturbImg
{
    
    if (!_disturbImg) {
        _disturbImg = [[UIImageView alloc] init];
        //        _disturbImg.contentMode = UIViewContentModeScaleAspectFill;
        _disturbImg.image = [UIImage imageNamed:@"chatDisturb_image"];
        _disturbImg.hidden = YES;
    }
    return _disturbImg;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
